layui.use(['table', 'admin', 'ax','laydate','form'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;
    var form = layui.form;

    laydate.render({
        elem: '#startTime'
        ,value:new Date()
    });

    laydate.render({
        elem: '#endTime'
        ,value:new Date()
    });

    /**
     * 运费成本差异管理
     */
    var BiCostDiff = {
        tableId: "biCostDiffTable"
    };

    /**
     * 初始化表格的列
     */
    BiCostDiff.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'dispatchNumber', sort: true, title: '调度单号', style:'font-weight: 600;'},
            {field: 'transportNumber', sort: true, title: '货运订单号', style:'font-weight: 600;'},
            {field: 'customerOrder', sort: true, title: '客户单号', style:'font-weight: 600;'},
            {field: 'carnumber', sort: true, title: '车牌号', style:'font-weight: 600;'},
            {field: 'toovolume', sort: true, title: '合计体积', style:'font-weight: 600;'},
            {field: 'mileageTraveled', sort: true, title: '行驶里程', style:'font-weight: 600;'},
            {field: 'chengyunfangshi', sort: true, title: '承运方式', style:'font-weight: 600;'},
            {field: 'carrierName', sort: true, title: '承运商', style:'font-weight: 600;'},
            {field: 'siji', sort: true, title: '司机', style:'font-weight: 600;'},
            {field: 'sijidianhua', sort: true, title: '司机电话', style:'font-weight: 600;'},
            {field: 'freightMoney', sort: true, title: '实收运费', style:'font-weight: 600;'},
            // {field: 'money', sort: true, title: '折扣运费', style:'font-weight: 600;'},
            {field: 'price', sort: true, title: '标准单价', style:'font-weight: 600;'},
            {field: 'standardCost', sort: true, title: '标准运费', style:'font-weight: 600;'},
            {field: 'diff', sort: true, title: '运费差异', style:'font-weight: 600;'},
        ]];
    };

    /**
     * 点击查询按钮
     */
    BiCostDiff.search = function () {
        var queryData = {};
        queryData['proId'] = $("#operateId").val();
        queryData['startTime'] = $("#startTime").val();
        queryData['endTime'] = $("#endTime").val();
        queryData['dispatchNumber'] = $("#dispatchNumber").val();
        queryData['customerOrder'] = $("#customerOrder").val();
        queryData['carnumber'] = $("#carnumber").val();
        table.reload(BiCostDiff.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
                 // 搜索按钮点击事件
                 // $('#btnSearch').click(function () {
                 //     BiCostDiff.search();
                 // });
                 // // 导出excel
                 // $('#btnExp').click(function () {
                 //     BiCostDiff.exportExcel();
                 // });
            }
        });
    };

    /**
     * 导出excel按钮
     */
    BiCostDiff.exportExcel = function () {
        var checkRows = table.checkStatus(BiCostDiff.tableId);
        if (checkRows.data.length > 0) {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        } else {
            var queryData = {};
            queryData['proId'] = $("#operateId").val();
            queryData['startTime'] = $("#startTime").val();
            queryData['endTime'] = $("#endTime").val();
            queryData['dispatchNumber'] = $("#dispatchNumber").val();
            queryData['customerOrder'] = $("#customerOrder").val();
            queryData['carnumber'] = $("#carnumber").val();
            $.ajax({
                type: "POST",
                url: Feng.ctxPath + "/biCostDiff/listAll",
                dataType:'json',
                data: queryData,
                beforeSend: function () {
                    //弹出加载中
                    index = layer.load(1, {
                        shade: [0.3, '#000'] //0.1透明度的白色背景
                    });
                },
                success: function (data) {
                    checkRows.data = data;
                    table.exportFile(tableResult.config.id, checkRows.data, 'xls');
                    layer.close(index);
                },
                error: function (data) {
                    console.log(data)
                }
            });
        }
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BiCostDiff.tableId,
        url: Feng.ctxPath + '/biCostDiff/list',
        page: true,
        limit: 20,
        height: "full-60",
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: BiCostDiff.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BiCostDiff.search();
    });

    // 导出excel
    $('#btnExp').click(function () {
        BiCostDiff.exportExcel();
    });

});
